<template>
	<view class="content">
		<view v-if="pageType == 'auth'">
			<view class="solids-bottom padding-xs flex align-center">
				<view class="flex-sub text-center">
					<view class="solid-bottom text-sl padding"><text class=" cuIcon-roundcheckfill text-blue"></text></view>
					<view class="padding">我们需要您的授权，授权登陆后可以正常使用平台提供的在线报修、物业缴费、停车缴费等功能</view>
				</view>
			</view>
			<!-- #ifdef MP-ALIPAY -->
			<view class="box margin-top-20" style=" padding: 20upx;">
				<button @click="aliAuth" class="cu-btn block bg-blue margin-tb-sm lg">
					<text :class="loadding2 == 1 ? 'cuIcon-loading2 cuIconfont-spin' : 'cuIconfont-spin'"></text>
					确认授权
				</button>
			</view>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<view class="box margin-top-20" style=" padding: 20upx;">
				<button class="cu-btn block bg-blue margin-tb-sm lg" open-type="getUserInfo" @getuserinfo="getuserinfo"
withCredentials="true">
					<text :class="loadding2 == 1 ? 'cuIcon-loading2 cuIconfont-spin' : 'cuIconfont-spin'"></text>
					确认授权 
				</button>
				
				<button class="cu-btn block  margin-tb-sm lg"  @click="cancleLogin">
					<text class="cuIconfont-spin"></text>
					暂不登陆 
				</button>
			</view>
			<!-- #endif -->
		</view>
		<form v-else>
			<view class="cu-form-group">
				<view class="title">手机号码</view>
				<input placeholder="输入手机号码" type="number" @input="setPhoneNum" name="input" />
				<view class="cu-capsule radius">
					<view class="cu-tag bg-blue ">+86</view>
					<view class="cu-tag line-blue">中国大陆</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">验证码</view>
				<input @input="setPhoneCode" type="number" placeholder="输入验证码" name="input" />
				<button @click="getCode" class="cu-btn bg-green shadow">{{ getCodeText }}</button>
			</view>

			<view class="box margin-top-20" style=" padding: 20upx;">
				<button @click="dobindphone" class="cu-btn block bg-blue margin-tb-sm lg">
					<text :class="loadding == 1 ? 'cuIcon-loading2 cuIconfont-spin' : 'cuIconfont-spin'"></text>
					保存手机号
				</button>
			</view>
		</form>
	</view>
</template>

<script>
import beeApp from '../../beeApp.js';
export default {
	data() {
		return {
			title: 'Hello',
			getCodeText: '获取验证码',
			sendingcode: 0,
			loadding: 0,
			pageType: 'bindphone',
			loadding2: 0,
			canIUse: wx ? wx.canIUse('button.open-type.getUserInfo') : 0
		};
	},
	onLoad(option) {
		if (option.page) {
			this.pageType = option.page;
		}
		if (this.pageType == 'auth') {
			uni.setNavigationBarTitle({
				title: '授权登陆'
			});
		}
		// beeApp.alert("yiqwyei");
	},
	methods: {
		cancleLogin:function(){
		  beeApp.data.getUserInfoCb = [];
		  uni.navigateBack()	
		},
		getCode: function() {
			var i = 60;
			var that = this;
			// var that = $("#codeBnt");
			if (this.sendingcode == 1) {
				return;
			}
			this.sendingcode = 1;
			//验证码倒计时

			that.getCodeText = '重新获取(' + i + 's)';
			var t = setInterval(function() {
				i--;

				that.getCodeText = '重新获取(' + i + 's)';
				if (i === 0) {
					clearInterval(t);
					that.getCodeText = '获取验证码';
					that.sendingcode = 0;
				}
			}, 1000);

			//加密
			beeApp.api('apiv2/user/getPhoneCode', { phone: that.phoneNum }, function(res) {
				if (res.code != 200) {
					beeApp.alert(res.msg);
					that.sendingcode = 0;
					clearInterval(t);
					that.getCodeText = '获取验证码';
					return;
				}
			});
		},

		setPhoneNum: function(e) {
			// console.log(e);
			this.phoneNum = e.detail.value;
		},

		getuserinfo: function(e) {
			console.log("wxAuth",e)
			if (this.loadding2 == 1) {
				return;
			}
			this.loadding2 = 1;
			if (e.detail.errMsg != 'getUserInfo:ok') {
				beeApp.data.getUserInfoCb = [];
				beeApp.alert("授权登陆失败："+e.detail.errMsg);
				this.loadding2 = 0;
				// wx.switchTab({
				// 			url: '/pages/index/index'
				// 		});
				uni.navigateBack();
				
			} else {
				this.userInfo = e.detail;
				this.login(this.userInfo);
				beeApp.setPageData('pages/user/index', {reload:1});
			}
		},
		login: function(postData) {
			var that = this;
			console.log(postData);
			beeApp.loginByUserInfo(postData, function(res) {
				// console.log('loginByUserInfo', res);
				that.loadding2 = 1;
				if (res.code != 200) {
					app.alert(res.msg);
				} else {
					// var backUrl = wx.getStorageSync('backUrl');
					if(beeApp.data.loginBack==1){
						beeApp.data.loginBack=0;
						uni.navigateBack()
					}else if (beeApp.data.path && beeApp.data.path!="pages/index/index") {
						var path = '/' + beeApp.data.path;
						beeApp.data.path = '';
						uni.navigateTo({
						   url: path
						});
					} else {
						uni.navigateBack();
					}
				}
			});
		},
		aliAuth: function() {
			var that = this;
			if (that.loadding2 == 1) {
				return;
			}
			that.loadding2 = 1;
			my.getAuthCode({
				scopes: 'auth_user',
				success: res => {
					// that.authCode = res.authCode;
					// that.loadding2=0;
					my.getAuthUserInfo({
						success: userInfo => {
							userInfo.authCode = res.authCode;
							beeApp.loginByUserInfo(userInfo);
							if (beeApp.data.path && beeApp.data.path!="pages/index/index") {
								var path = '/' + beeApp.data.path;
								beeApp.data.path = '';
								// uni.navigateBack()
								console.log('redirectTo');
								uni.redirectTo({
									url: path
								});
							} else {
								uni.navigateBack();
							}

							that.loadding2 = 0;
						}
					});
				},
				fail: function(res) {
					console.log('getAuthCode:', res);
					that.loadding2 = 0;
				}
			});
		},
		setPhoneCode: function(e) {
			// console.log(e);
			this.phoneCode = e.detail.value;
		},
		dobindphone: function() {
			var that = this;
			that.loadding = 1;
			var user_info = uni.getStorageSync('user_info');
			beeApp.api(
				'apiv2/user/bindPhone',
				{
					phone: this.phoneNum,
					code: this.phoneCode,
					openid: user_info.openid,
					method: 'POST'
				},
				function(res) {
					that.loadding = 0;
					if (res.code != 200) {
						beeApp.alert(res.msg);
						return;
					}
					res.data.openid = user_info.openid;
					uni.removeStorageSync('user_info');
					uni.setStorageSync('user_info', res.data);
					beeApp.setPageData('pages/user/index', { reload: 1 });
					uni.navigateBack();
				}
			);
		}
	}
};
</script>
